﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bon_It.Model
{
    using Bon_It.Resources;

    public partial class Artikel : EntityBase<Artikel>
    {

        /// <summary>
        /// Attach the property changed method from the base class.
        /// </summary>
        partial void OnCreated()
        {
            PropertyChanged += OnPropertyChanged;

        }

        private bool IsInputValid(string input)
        {
            foreach (Char c in input)
            {

                if (Char.IsDigit(c) ==false && c !='.' && c.ToString() != System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)
                {
                    return false;
                };
            }

            return true;

        }

        public override string this[string columnName]
        {
            get
            {
                switch (columnName)
                {
                    case "VKPreis":
                    case "Bestand":
                        if (!IsInputValid(Bestand.ToString()))
                        {
                            return string.Concat(ErrorResources.ErrornousInput,"\n", ErrorResources.OnlyDecumalNumbersAreAllowd);
                        }
                        break;
                    case "fkTaxID":
                        if (fkTaxID == 0)
                        {
                            return ErrorResources.TaxrateIsMissing;
                        }
                        break;
                    case "fkWarengruppe":
                        if (fkWarengruppe == 0)
                        {
                            return ErrorResources.CommodityGroupIsMissing;
                        }
                        break;
                    case "fkGebindeID":
                        if (fkGebindeID == 0)
                        {
                            return ErrorResources.PackagingIsMissing;
                        }
                        break;
                    case "EANNummer":
                        if (EANNummer.ToString().Length<4)
                        {
                            return ErrorResources.ArticleNumberMustcontainsMoreThanThreeNumbers;
                        }
                        break;
                    case "Bezeichnung":
                        if (string.IsNullOrWhiteSpace(Bezeichnung))
                        {
                            return ErrorResources.ArticleNameIsMissing;
                        }
                        break;
                }


                return base[columnName];
            }
        }

        public override string Error
        {
            get
            {

                if (

                    string.IsNullOrEmpty(this["Bezeichnung"]) == false ||
                    string.IsNullOrEmpty(this["VKPreis"]) == false || 
                    string.IsNullOrEmpty(this["Bestand"]) == false || 
                    string.IsNullOrEmpty(this["fkTaxID"]) == false || 
                    string.IsNullOrEmpty(this["fkWarengruppe"]) == false || 
                    string.IsNullOrEmpty(this["fkGebinde"]) == false || 
                    string.IsNullOrEmpty(this["EANNummer"]) == false 
                    )
                {
                    return "error";
                }
                return string.Empty;
            }
        }
    }
}
